package com.wgt.upload.wrapper;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.service.FastFileStorageClient;
import com.wgt.upload.bean.FileConfigBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author wgt
 * @date 2018/1/10
 * @project fastdfs-server
 */
@Component
public class FastDFSClientWrapper {

    @Autowired
    private FastFileStorageClient storageClient;

    @Autowired
    private FileConfigBean configBean;


    public String uploadFile(String filePath,String fileType) throws IOException {
        File file = new File(filePath);
        InputStream is = new FileInputStream(file);
        
        //依次传入 1文件流  2文件大小 3文件类型 4meta元素 （可有可无）
        StorePath storePath = storageClient.uploadFile(is,file.length(),fileType.substring(1,fileType.length()),null);
        return getResAccessUrl(storePath);
    }



    // 封装文件完整URL地址
    private String getResAccessUrl(StorePath storePath) {
        String fileUrl = configBean.getServerIp() + "/" + storePath.getFullPath();
        return fileUrl;
    }
}
